//
//  LPTWebViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/6.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "LPTWebViewController.h"
#import <WebKit/WKWebView.h>
#import <WebKit/WKNavigationDelegate.h>
#import "MMProgressHUD.h"

@interface LPTWebViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) IBOutlet WKWebView *webView;
@property (nonatomic, strong) NSString *m_url;

@end

@implementation LPTWebViewController


+ (instancetype)instanceWithUrl:(NSString *)url
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"WebVC" bundle:nil];
    LPTWebViewController *vc = [storyBoard instantiateInitialViewController];
    vc.m_url = url;
    return vc;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    [MMProgressHUD show];
    
    _webView.navigationDelegate = self;
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.m_url]];
    [_webView loadRequest:request];
    
}

/// 2 页面开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    
}
/// 4 开始获取到网页内容时返回
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    [self.webView evaluateJavaScript:@"document.title" completionHandler:^(id object, NSError * error) {
        
        self.title = object;
        
    }];
    
}

/// 5 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [MMProgressHUD dismiss];
    
}

/// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [MMProgressHUD dismissWithError:@"加载失败"];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
